package com.jysz.certificate.service.cs;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.cs.CheckStockDao;
import com.jysz.certificate.persistence.dao.cs.CheckStockOptionDao;
import com.jysz.certificate.persistence.model.cm.Certification;
import com.jysz.certificate.persistence.model.cs.CheckStockOption;
import com.jysz.certificate.utils.CommonUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

/**
 *
 * @author xuxiaolong
 */
@Service
public class CheckStockOptionService extends EntityService<CheckStockOption, Long, CheckStockOptionDao> {

	@Resource
	private CheckStockOptionDao optionDao;
	@Resource
	private CheckStockDao csDao;
	

	@Override
	public CheckStockOptionDao getEntityDao() {
		return optionDao;
	}
	
	public Page<CheckStockOption> findPage(Long csId,CheckStockOption option,int pageNumber,int pageSize){
		
		Criteria criteria=optionDao.createCriteria();
		Criteria cfCriteria=criteria.createCriteria("certification");
		cfCriteria.addOrder(Order.asc("brand"));
		cfCriteria.addOrder(Order.asc("financial"));
		cfCriteria.addOrder(Order.desc("warehouseDate"));
		
		criteria.createCriteria("checkStock").add(Restrictions.eq("id", csId));
		if(option!=null&&option.getCertification()!=null){
			Certification cf=option.getCertification();
//			Criteria cfCriteria= criteria.createCriteria("certification");
			//主机厂
			if(cf.getBrand()!=null&&CommonUtil.isNotEmpty(cf.getBrand().getName()))
				cfCriteria.createCriteria("brand").add(Restrictions.like("name", cf.getBrand().getName(),MatchMode.ANYWHERE));
			//金融机构
			if(cf.getFinancial()!=null&&CommonUtil.isNotEmpty(cf.getFinancial().getName()))
				cfCriteria.createCriteria("financial").add(Restrictions.like("name", cf.getFinancial().getName(),MatchMode.ANYWHERE));
			//合格证号
			if(CommonUtil.isNotEmpty(cf.getCertificationNum()))
				cfCriteria.add(Restrictions.like("certificationNum",cf.getCertificationNum(),MatchMode.ANYWHERE));
			//车架号
			if(CommonUtil.isNotEmpty(cf.getChassis()))
				cfCriteria.add(Restrictions.like("chassis",cf.getChassis(),MatchMode.ANYWHERE));
		}
		
		return this.findPage(criteria, pageNumber, pageSize);
	}
	
	public List<CheckStockOption> findList(Long csId,CheckStockOption option){
		
		Criteria criteria=optionDao.createCriteria();
		Criteria cfCriteria=criteria.createCriteria("certification");
		cfCriteria.addOrder(Order.asc("brand"));
		cfCriteria.addOrder(Order.asc("financial"));
		cfCriteria.addOrder(Order.desc("warehouseDate"));
		
		criteria.createCriteria("checkStock").add(Restrictions.eq("id", csId));
		if(option!=null&&option.getCertification()!=null){
			Certification cf=option.getCertification();
//			Criteria cfCriteria= criteria.createCriteria("certification");
			//主机厂
			if(cf.getBrand()!=null&&CommonUtil.isNotEmpty(cf.getBrand().getName()))
				cfCriteria.createCriteria("brand").add(Restrictions.like("name", cf.getBrand().getName(),MatchMode.ANYWHERE));
			//金融机构
			if(cf.getFinancial()!=null&&CommonUtil.isNotEmpty(cf.getFinancial().getName()))
				cfCriteria.createCriteria("financial").add(Restrictions.like("name", cf.getFinancial().getName(),MatchMode.ANYWHERE));
			//合格证号
			if(CommonUtil.isNotEmpty(cf.getCertificationNum()))
				cfCriteria.add(Restrictions.like("certificationNum",cf.getCertificationNum(),MatchMode.ANYWHERE));
			//车架号
			if(CommonUtil.isNotEmpty(cf.getChassis()))
				cfCriteria.add(Restrictions.like("chassis",cf.getChassis(),MatchMode.ANYWHERE));
		}
		return criteria.list();
	}

}
